package controller;

import model.Afd;
import model.Transition;
import model.State;
import model.Symbol;

public class ComputeWordController {
	private static ComputeWordController computeWordController;
	
	private ComputeWordController(){
	}
	
	public static ComputeWordController getInstance(){
		if(computeWordController == null){
			computeWordController = new ComputeWordController();
		}		
		return computeWordController;
	}
	
	public String computeWord(String word) {
		Afd afd = AfdController.getInstance().getAfd();
		
		State currentState = afd.getInitialState();
		StringBuilder sb = new StringBuilder("(");
		sb.append(currentState).append(",").append(word).append(")");
		int i = 1;
		for (char c : word.toCharArray()) {
			Symbol symbol = new Symbol(c);
			for (Transition r : afd.getTransitions()) {
				if(r.getInitialState().equals(currentState) && r.getSymbol().equals(symbol)){
					currentState = r.getFinalState();
					sb.append("|-(").append(currentState).append(",");
					if(!word.substring(i).equals("")){
						sb.append(word.substring(i)).append(")");
					}else{
						sb.append("ɛ").append(")");
					}
					break;
				}
			}
			i++;
		}
		if(afd.getFinalStates().contains(currentState))
			sb.append("\nPalavra Foi Aceita");
		else
			sb.append("\nPalavra Foi Rejeitada");
		return sb.toString();
	}
	
}
